package com.yonyou;


import com.yonyou.config.MyBatisConfig;
import com.yonyou.mapper.UserMapper;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MainStarter {

    public static void main(String[] args) {
    	// 记载spring上下文
        //开启事务 Connection TransactionSynchronizationManager.resource
        AnnotationConfigApplicationContext ioc = new AnnotationConfigApplicationContext(MyBatisConfig.class);
        //mybatis集成spring的事务：只要拿到spring的 Connection 就ok
        //org.mybatis.spring.transaction.SpringManagedTransaction.getConnection 该方法最终会去事务同步器拿连接
        //集成spring时 SpringManagedTransactionFactory -> SpringManagedTransaction -> SpringManagedTransaction.getConnection
        // -> SpringManagedTransaction.openConnection -> DataSourceUtils.getConnection ->
        // org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection
		UserMapper bean = (UserMapper) ioc.getBean("userMapper");
        System.out.println(bean.selectById(1L));

        //
		/*UserMapper userMapper =(UserMapper) Proxy.newProxyInstance(UserMapper.class.getClassLoader(), new Class[]{UserMapper.class}, (proxy, method, args1) -> {
			// 查询数据库...
			System.out.println("查询");
			return new User();
		});
		userMapper.selectById(1L);*/
	}
}
